package com.legsem.jhosync.ws.rs;

import java.io.ByteArrayOutputStream;
import java.io.OutputStreamWriter;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.legsem.jhosync.Server;
import com.legsem.jhosync.dto.ClientCreate;

@Path("clientreset")
public class ClientResetResource extends AbstractResource {

    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8")
    public Response clientCreate(@QueryParam("client_id") String client_id,
            @Context HttpServletRequest req) {
        try {
            Server server = getServer(req);
            ClientCreate clientCreate = server.doReset(client_id);
            ByteArrayOutputStream osba = new ByteArrayOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(osba, "UTF-8");
            getMapper().writeValue(osw, clientCreate);
            osw.close();
            return Response.ok(osba.toByteArray()).build();
        } catch (Exception e) {
            throw new WebApplicationException(e,
                    Response.Status.INTERNAL_SERVER_ERROR);
        }

    }

}
